﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace LostLib.Programs.AssemblyInfos
{
	/// <summary>
	/// Represents a Assembly dependency.
	/// </summary>
	[Serializable]
	public class AssemblyDependency
	{
		/// <summary>
		/// Constructs AssemblyDependency object.
		/// </summary>
		/// <param name="Assembly">Required Assembly name.</param>
		/// <param name="version">Required Assembly version.</param>
		public AssemblyDependency(string assembly, DateTime version)
		{
			Assembly = assembly;
			Version = version;
		}

		/// <summary>
		/// Required Assembly name.
		/// </summary>
		public string Assembly { get; private set; }
		/// <summary>
		/// Required Assembly version.
		/// </summary>
		public DateTime Version { get; private set; }

		//static readonly Regex regexDependency = new Regex(
		//    @"^\s*(?<name>[a-zA-Z\-_0-9]+)\s*\[\s*(?<version>[0-9_\s\.\\\/\-]*)\s*\](?<rest>.*)$",
		//    RegexOptions.Compiled | RegexOptions.Singleline);

		//static AssemblyDependency Parse(ref string dependancies)
		//{
		//    var match = regexDependency.Match(dependancies);

		//    if (!match.Success) throw new FormatException();

		//    var Assembly = match.Groups["name"].Value;
		//    var version = DateTime.Parse(match.Groups["version"].Value);
		//    dependancies = match.Groups["rest"].Value.Trim();
		//    return new AssemblyDependency(Assembly, version);
		//}

		/// <summary>
		/// Converts AssemblyDependency to its string representation.
		/// </summary>
		/// <returns>String representation.</returns>
		public override string ToString()
		{
			return string.Format(CultureInfo.CurrentCulture, "{0}[{1}]", Assembly, Version.ToShortDateString());
		}
	}

	///// <summary>
	///// List of Assembly dependencies.
	///// </summary>
	//public class AssemblyDependencyList
	//{
	//    /// <summary>
	//    /// Constructs an instance of AssemblyDependencyList.
	//    /// </summary>
	//    /// <param name="dependencies">List of dependencies.</param>
	//    public AssemblyDependencyList(params AssemblyDependency[] dependencies)
	//    {
	//        _dependencies = new List<AssemblyDependency>(dependencies);
	//        _or = false;
	//    }
	//    /// <summary>
	//    /// Constructs an instance of AssemblyDependencyList.
	//    /// </summary>
	//    /// <param name="dependencies">List of dependencies.</param>
	//    /// <param name="or"></param>
	//    public AssemblyDependencyList(bool or, params AssemblyDependency[] dependencies)
	//    {
	//        _dependencies = new List<AssemblyDependency>(dependencies);
	//        _or = or;
	//    }

	//    List<AssemblyDependency> _dependencies;
	//    bool _or;

	//    public IList<AssemblyDependency> Dependencies
	//    {
	//        get { return _dependencies; }
	//    }
	//    public bool Or
	//    {
	//        get { return _or; }
	//    }

	//    public override string ToString()
	//    {
	//        if (_dependencies.Count == 0) return "";
	//        if (_dependencies.Count == 1) return Dependencies[0].ToString();
	//        var sb = new StringBuilder();
	//        if (Or) sb.Append("or ");

	//        foreach (var dependency in _dependencies)
	//            sb.AppendFormat("{0} ", dependency);

	//        return sb.ToString().TrimEnd(' ');
	//    }

	//    public static AssemblyDependencyList Parse(string dependancies)
	//    {
	//        dependancies = dependancies.Trim();
	//        bool or = false;
	//        if (dependancies.StartsWith("or "))
	//        {
	//            or = true;
	//            dependancies = dependancies.Substring(3).TrimStart(' ');
	//        }
	//        var deps = new List<AssemblyDependency>();
	//        while (dependancies.Length > 0)
	//        {
	//            deps.Add(AssemblyDependency.Parse(ref dependancies));	
	//        }

	//        return new AssemblyDependencyList(or, deps.ToArray());
	//    }
	//}
}
